import Watcher from './watcher.js'

const compileProp = (vm, prop) => {
  if (!prop.includes('.')) return vm[prop]
  // 处理嵌套对象
  const objProps = prop.split('.')
  let initObj = vm
  objProps.forEach(p => {
    initObj = initObj[p]
  })
  return initObj
}

const setPropValue = (vm, prop, newVal) => {
  if (!prop.includes('.')) {
    vm[prop] = newVal
    return
  }
  // 处理嵌套对象
  const objProps = prop.split('.')
  let initObj = vm
  objProps.forEach((p, index) => {
    if (index === objProps.length - 1) {
      initObj[p] = newVal
    } else {
      initObj = initObj[p]
    }
  })
}

function getEventArgs () {
  console.log(arguments)
  return Array.from(arguments)
}
class Compiler {
  constructor (vm) {
    this.el = vm.$el
    this.vm = vm
    this.compile(this.el)
  }

  compile (el) {
    const childNodes = el.childNodes
    Array.from(childNodes).forEach(node => {
      if (this.isTextNode(node)) {
        this.compileText(node)
      }

      if (this.isElementNode(node)) {
        this.compileElement(node)
      }

      // 处理深层次子节点
      if (node.childNodes && node.childNodes.length) {
        this.compile(node)
      }
    })
  }

  compileElement(node) {
    const { attributes } = node
    Array.from(attributes).forEach(attr => {
      let attrName = attr.name
      if (this.isDirective(attrName)) {
        attrName = attrName.substr(2)
        const { value } = attr
        if (attrName.startsWith('on')) {
          this.onHandle(node, value, attrName)
        } else {
          this.update(node, value, attrName)
        } 
      }
    })
  }

  update (node, prop, attrName) {
    const updateFn = this[`${attrName}Updater`]
    const val = compileProp(this.vm, prop)
    updateFn && updateFn.call(this, node, val, prop)
  }

  // v-text
  textUpdater (node, value, prop) {
    node.textContent = value

    // 创建watcher对象监听数据改变
    new Watcher(this.vm, prop, newValue => {
      node.textContent = newValue
    })
  }

  // v-model
  modelUpdater (node, value, prop) {
    node.value = value

    // 创建watcher对象监听数据改变
    new Watcher(this.vm, prop, newValue => {
      node.value = newValue
    })

    // 实现v-model双向绑定
    node.addEventListener('input', () => {
      setPropValue(this.vm, prop, node.value)
    })
  }

  // v-html
  htmlUpdater (node, value, prop) {
    node.innerHTML = value

    // 创建watcher对象监听数据改变
    new Watcher(this.vm, prop, newValue => {
      node.innerHTML = newValue
    })
  }

  // v-on
  onHandle (node, fnName, prop) {
    const fnType = prop.split(':')[1]
    const { methods } = this.vm.$options
    this.vm[fnName] = methods[fnName].bind(this.vm)
    node.addEventListener(fnType, () => {
      this.vm[fnName] && this.vm[fnName]()
    })
  }

  compileText(node) {
    // 匹配{{}}
    const reg = /\{\{(.+?)\}\}/
    let value = node.textContent
    if (reg.test(value)) {
      // 获取正则中的第一个分组
      const prop = RegExp.$1.trim()
      const val = compileProp(this.vm, prop)
      node.textContent = value.replace(reg, val)

      // 创建watcher对象监听数据改变
      new Watcher(this.vm, prop, newValue => {
        node.textContent = newValue
      })
    }
  }

  isDirective(attrName) {
    return attrName.startsWith('v-')
  }

  isTextNode(node) {
    return node.nodeType === 3
  }

  isElementNode(node) {
    return node.nodeType === 1
  }
}

export default Compiler
export {
  compileProp,
}
